﻿using Hardcodet.Wpf.TaskbarNotification;
using Newtonsoft.Json;
using Qins.PluginInterface;
using System.Windows;
using System.Windows.Input;

namespace Qins.Plugins.NotifyIcon
{
    
    public class QinNotifyIcon : Window, IPlugin
    {
        public QinNotifyIcon()
        {
            this.Name= "QinNotifyIcon";
        }
        private TaskbarIcon? notifyIcon;
        public string PluginName => "QinNotifyIcon";
        public string Description { get => "在系统托盘显示小图标"; }

        public string InDescription { get => "GetAllPlugin"; }

        public string OutDescription { get => "显示设置"; }

        public bool AutoRun { get => true; }

        private List<PluginInfo> lpi;

        public string Execute(object inPars)
        {
            if (inPars == null)
            {
                var NotifyIconresource = new ResourceDictionary()
                {
                    Source = new Uri("pack://Application:,,,/Qins.Plugins.NotifyIcon;component/NotifyIconResources.xaml")
                };
                Resources.MergedDictionaries.Clear();
                Resources.MergedDictionaries.Add(NotifyIconresource);
                //ResourceDictionary resourceDictionary = new ResourceDictionary(); 
                //Application.LoadComponent(resourceDictionary, new Uri("pack://Application:,,,/Qins.Plugins.NotifyIcon;component/NotifyIconResources.xaml")); 
                //Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
                notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");
                return ("Success loaded");
            }
            else
            {
                var mess = (MessageContent)inPars;
                lpi = JsonConvert.DeserializeObject<List<PluginInfo>>(mess.content?.ToString());
                PluginManager pm = new PluginManager(this,lpi);
                pm.Show();
                return "ok";
            }
            return "no";
        }

        public Message MessageTransport { get; set; }

        public void Initialization(Message messageTransport)
        {
            MessageTransport = messageTransport;
            MessageTransport.Receive += CallPluginRun;//绑定接收方法
        }

        public string CallPluginRun(string pluginName, MessageContent message)
        {
            string reSend= MessageTransport.SendMessage(pluginName, message);
            return reSend;
        }
    }
}